package com.eattravel.interceptor;

import com.eattravel.vo.ResultData;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登陆拦截：
 *     拦截需要登陆的页面，必须经过此处，判断是否登陆过    拦截清单
 *     不需要登陆就可以访问的页面，要配置到放行的清单中    放行清单
 */
public class LoginInterceptor implements HandlerInterceptor {

    //  在资源被访问前执行
    // 返回true,表示放行，继续向后执行
    // 返回false，表示拦截此次请求，不会访问到目标资源
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       System.out.println("============ Interceptor ===========");
        HttpSession session = request.getSession();
        Object object = session.getAttribute("userName");
        if(null == object) {


           // 没有登陆
           response.sendRedirect("/error/403.html");
           return false;
       }

        return true;
    }

    // 在资源被访问后执行
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }
    // preHandle 返回true,afterCompletion  一定执行
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
